<template>
    <!-- <view> -->
    <view class="container">
        <view class="wechatapp">
            <!-- <image class="header" src="/images/wechatapp.png"></image> -->
            <view class="header">
                <open-data class="" type="userAvatarUrl"></open-data>
            </view>
        </view>
        <view class="auth-title">申请获取以下权限</view>
        <view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
        <button class="login-btn" openType="getUserInfo" lang="zh_CN" @getuserinfo="authorLogin">授权登录</button>
    </view>
    <!-- </view> -->
</template>

<script>
let app = getApp();
export default {
    data() {
        return {};
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {},
    onShow: function () {
        //console.log(wx.getStorageSync('user_id'))
    },
    methods: {
        /**
         * 授权登录
         */
        authorLogin: function (e) {
            let that = this;
            if (e.detail.errMsg !== 'getUserInfo:ok') {
                return false;
            }
            uni.showLoading({
                title: '正在登录',
                mask: true
            });
            // 执行微信登录
            uni.login({
                success: function (res) {
                    // 发送用户信息
                    app.globalData._post_form(
                        'user/login',
                        {
                            code: res.code,
                            //app_id: App.siteInfo.appid,
                            user_info: e.detail.rawData,
                            encrypted_data: e.detail.encryptedData,
                            iv: e.detail.iv,
                            signature: e.detail.signature,
                            referee_id: uni.getStorageSync('referee_id')
                        },
                        function (result) {
                            // 记录token user_id
                            uni.setStorageSync('token', result.data.token);
                            uni.setStorageSync('user_id', result.data.user_id);
                            uni.setStorageSync('user_info', e.detail.rawData);

                            // 跳转回原页面
                            //wx.setStorageSync("hasToLogin", null);
                            that.navigateBack();
                        },
                        false,
                        function () {
                            uni.hideLoading();
                        }
                    );
                }
            });
        },

        /**
         * 授权成功 跳转回原页面
         */
        navigateBack: function () {
            uni.navigateBack();
            let currentPage = uni.getStorageSync('currentPage');
            //console.log('ccc',currentPage)
            /* 		wx.redirectTo({
                  url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options)
              }); */
            uni.redirectTo({
                url: '/pages/index/index'
            });
        }
    }
};
</script>
<style>
@import './login.css';
</style>
